package com.yyplatform.admin.platform.file.utils;

import org.apache.commons.lang3.StringUtils;

import jakarta.servlet.http.HttpServletRequest;

/**
 * @author sleepforests@163.com
 * <p>
 * Created on 2024/9/24 22:18
 */
public class FileUrlUtils {

    public static String replaceUrl(String url, HttpServletRequest request) {
        String prefix = request.getScheme() + "://" + request.getServerName()
                + ":" + request.getServerPort();

        return StringUtils.replace(url, "LOCALHOST", prefix);
    }


    public static String addLastSlash(String url) {
        if (StringUtils.isBlank(url)) {
            return url;
        }
        if (!url.endsWith("/")) {
            url += "/";
        }
        return url;
    }


    public static String delFirstSlash(String url) {
        if (StringUtils.isBlank(url)) {
            return url;
        }
        if (url.startsWith("/")) {
            url = url.substring(1);
        }
        return url;
    }


    public static String delFirstAddLastSlash(String url) {
        String s1 = delFirstSlash(url);
        return addLastSlash(s1);
    }


}
